package batframework.mvc.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import batframework.code.util.ClassUtils;
import batframework.code.util.IoUtils;
import batframework.mvc.handler.ActionHandler;
import batframework.mvc.handler.Handler;
import batframework.mvc.web.WebContext;


@WebServlet("/")
public class DispatcherServlet extends HttpServlet
{
	private static final long serialVersionUID = 6850762750317244531L;
	
	@Override
	public void init(ServletConfig config) throws ServletException
	{
		/*String conf = config.getInitParameter("conf");
		if(StringUtils.isEmpty(conf))
			throw new IllegalArgumentException("conf class is required");
		Class<?> confClass = ClassUtils.forName(conf);
		if (confClass == null)
			throw new IllegalArgumentException("conf class not found");
		*/
	}
	
	@Override
	public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
	{
		long s1 = System.currentTimeMillis();
		WebContext context = new WebContext(request, response);
		
		Handler handler = new ActionHandler();
		
		handler.process(context);
		
		handler = null;
		
		context = null;
		
		String s = "elevenframework/conf/ApplicationConf.class";
		System.out.println(ClassUtils.getClassPath(s));
		System.out.println(IoUtils.fileExists(ClassUtils.getClassPath(s)));
		/*
		String classPath = request.getServletContext().getRealPath("/WEB-INF/classes");
		String path = request.getServletPath();
		System.out.println(classPath);
		System.out.println(path);
		String[] split = path.split("/");
		System.out.println(split.length);
		if(split.length == 0)
		{
			
		}
		if(split.length == 2)
		{
			
		}
		else
		{
			System.out.println(split[1]+"-----------");
			
		}
		boolean exists = FileUtils.isExists(classPath+"/elevenframework/mvc/Controller.class");
		System.out.println(exists+"===========");
		if (exists)
		{
			try
			{
				Class<?> clasz = Class.forName("elevenframework.mvc.controller.IndexController");
				
				System.out.println(clasz.getName());
				
				clasz = null;
			} catch (ClassNotFoundException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		long s2 = System.currentTimeMillis();
		System.out.println((s2-s1));
		
	}
	
	@Override
	public void destroy()
	{
		//application.destroy();
	}
	
	public static void main(String[] args)
	{
		String s = "elevenframework/conf/ApplicationConf.class";
		System.out.println(IoUtils.fileExists(ClassUtils.getClassPath(s)));
	}

}
